<template>
  <el-container class="container">
    <el-header>
      <div>
        <img src="./../assets/logo.png" height="60px" alt="logo">
        <span>电商后台管理</span>
      </div>
      <el-button type="info" @click="loginOut()">注销</el-button>
    </el-header>
    <el-container>
      <el-aside :width='isCollapse ? "64px" : "200px"'>
        <div :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" @click="toggleCollapse"></div>
        <!-- 路由链路跳转，给menu添加router，路由以index的值为路由跳转地址 -->
        <el-menu unique-opened background-color="#313743" text-color="#fff" active-text-color="#359BFF"
          :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单项 -->
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id + ''">
            <!-- 模板区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单选项区域 -->
            <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="'/' + subItem.path"
              @click="saveNavstate(subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main><router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconObj: {
        '125': 'el-icon-s-custom',
        '103': 'el-icon-monitor',
        '101': 'el-icon-shopping-bag-2',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-data',
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem("activePath")
  },
  methods: {
    // 清除token，并跳转到登录页面
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push('/login')
    },
    // 请求左侧管理列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('res.meta.msg')
      this.menulist = res.data
    },
    // 管理列表缩放
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存选中的二级菜单，在页面刷新的时候也能刷新展示本页
    // 与:default-active配合使用
    saveNavstate(activePath) {
      window.sessionStorage.activePath = activePath
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}

.el-header {
  background-color: #2D3133;
  display: flex;
  justify-content: space-between;
  align-items: center;

  >div {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 18px;

    span {
      margin-left: 20px;
    }
  }
}

.el-aside {
  background-color: #313741;

  .el-menu {
    border-right: 0;
  }

  >div {
    width: 100%;
    padding: 6px 0;
    text-align: center;
    font-size: 20px;
    color: #909399;
  }
}

.el-main {
  background-color: #E9EDF1;
  height: calc(100vh - 80px); //60px为顶部header盒子高度+20间距
  overflow-y: auto;
}
</style>